package com.summer.bilibili.util;

import com.summer.bilibili.consts.Code;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应体
 */
@Data
@AllArgsConstructor // 全参构造
public final class R< T > implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    /**
     * @param data
     * @param <T>
     * @return
     */
    public static < T > R < T > ok ( T data ) {
        return ok ( data , null );
    }

    public static < T > R < T > ok ( T data , String msg ) {
        return new R <> ( Code.OK.getValue ( ) , msg , data );
    }

    public static R < ? > error ( String msg ) {
        return new R <> ( Code.ERROR.getValue ( ) , msg , null );
    }

    public static R < ? > error ( Code code ) {
        return new R <> ( code.getValue ( ) , code.getMsg ( ) , null );
    }
}
